/*****************************************************************************
模块名  : CDlgBase
文件名  : CDlgBase.h
相关文件：
实现功能： widget基类
作者    ：
版本    ：
******************************************************************************/
#include "wgt_base.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////
// CWgtBase
CWgtBase::CWgtBase(s8* szDlgName, s8* szTitle, u8* szParam, s32 nLen, QWidget* pWidParent)
{
    if (m_byShowMode == EDLGSHOWMODE_POP || m_byShowMode == EDLGSHOWMODE_POP_SAVE)
    {
		setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
		setObjectName(m_strTitle);
		setWindowTitle(m_strTitle);
    }
    else
    {
		setWindowFlags(windowFlags() | Qt::SubWindow | Qt::FramelessWindowHint);
		setObjectName(m_strTitle);
		setWindowTitle(m_strTitle);
    }
    setAttribute(Qt::WA_DeleteOnClose);

	if (szDlgName != NULL)
		m_strName = (s8*)szDlgName;

	if (szParam != NULL)
		m_strParam = (s8*)szParam;

	if (szTitle != NULL)
		m_strTitle = szTitle;

}

CWgtBase::~CWgtBase()
{
	m_bInit = false;
}

bool CWgtBase::Init(void)
{
    if (m_bInit)
        return true;

	InitData();
	InitCtrl();
	InitEvt();

    UpdateUIFromData();
	m_bInit = true;

	return true;
}
bool CWgtBase::Release(void)
{
    if (!m_bInit)
        return false;

	m_bInit = false;
	return true;
}
bool CWgtBase::IsInit()
{
	return m_bInit;
}

//////////////////////////////////////////////////////////////////////////////////////
// 启动初始化
void CWgtBase::InitData(void)
{
}
void CWgtBase::InitCtrl(void)
{
}
void CWgtBase::InitEvt(void)
{
//	bool bRet = connect(this, &CWgtBase::sglUIDataChanged, this, &CWgtBase::sltUIDataChanged);
//	if (!bRet)
//	{
//		int m = 0;
//		m++;
//	}
}

//////////////////////////////////////////////////////////////////////////////////////
//更新界面上的各个部件
bool CWgtBase::UpdateDataFromUI(int nCtrlIndex)
{
    if (nCtrlIndex == 0)
    {
        map<int, TUIInfo>::iterator ite = m_mapCtrl.begin();
        while (ite != m_mapCtrl.end())
        {
            UpdateDataFromUI(ite->second);
            ite++;
        }
        return true;
    }
    else
    {
        map<int, TUIInfo>::iterator ite = m_mapCtrl.find(nCtrlIndex);
        if (ite != m_mapCtrl.end())
        {
            return UpdateDataFromUI(ite->second);
        }
        return false;
    }
}
bool CWgtBase::UpdateDataFromUI(TUIInfo& tCtrl)
{
    if (tCtrl.nType == EMCTRLTYPE_NULL || tCtrl.pCtrlObj == NULL || tCtrl.pValue == NULL)
    {
        return false;
    }

    if (tCtrl.nType == ECTRLTYPE_LBL_TXT)
    {
        QByteArray byteArray = ((QLabel*)tCtrl.pCtrlObj)->text().toLatin1();
        *((string*)tCtrl.pValue) = byteArray.data();
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_INT8)
    {
        *(u8*)tCtrl.pValue = ((QLabel*)tCtrl.pCtrlObj)->text().toInt();
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_INT16)
    {
        *(u16*)tCtrl.pValue = ((QLabel*)tCtrl.pCtrlObj)->text().toInt();
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_INT)
    {
        *(u32*)tCtrl.pValue = ((QLabel*)tCtrl.pCtrlObj)->text().toInt();
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_FLOAT)
    {
        *(u32*)tCtrl.pValue = ((QLabel*)tCtrl.pCtrlObj)->text().toFloat();
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_TXTUTF8)
    {
        QByteArray byteArray = ((QLineEdit*)tCtrl.pCtrlObj)->text().toUtf8();
        *((string*)tCtrl.pValue) = byteArray.data();
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_TXT)
    {
        QByteArray byteArray = ((QLineEdit*)tCtrl.pCtrlObj)->text().toLatin1();
        *((string*)tCtrl.pValue) = byteArray.data();
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_INT8)
    {
        *(u8*)tCtrl.pValue = ((QLineEdit*)tCtrl.pCtrlObj)->text().toInt();
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_INT16)
    {
        *(u16*)tCtrl.pValue = ((QLineEdit*)tCtrl.pCtrlObj)->text().toInt();
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_INT)
    {
        *(u32*)tCtrl.pValue = ((QLineEdit*)tCtrl.pCtrlObj)->text().toInt();
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_FLOAT || tCtrl.nType == ECTRLTYPE_EDT_FLOAT2)
    {
        *(float*)tCtrl.pValue = ((QLineEdit*)tCtrl.pCtrlObj)->text().toFloat();
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_DOUBLE)
    {
        *(double*)tCtrl.pValue = ((QLineEdit*)tCtrl.pCtrlObj)->text().toDouble();
    }
    else if (tCtrl.nType == ECTRLTYPE_RADIO_INT)
    {
        *(int*)tCtrl.pValue = ((QButtonGroup*)tCtrl.pCtrlObj)->checkedId();
    }
    else if (tCtrl.nType == ECTRLTYPE_RADIO_INT8)
    {
        *(u8*)tCtrl.pValue = ((QButtonGroup*)tCtrl.pCtrlObj)->checkedId();
    }
    else if (tCtrl.nType == ECTRLTYPE_RADIO_INT16)
    {
        *(u16*)tCtrl.pValue = ((QButtonGroup*)tCtrl.pCtrlObj)->checkedId();
    }
    else if (tCtrl.nType == ECTRLTYPE_IPD_INT)
    {
        //*(u32*)tCtrl.pValue = IpQStringToInt(((IpInputWidget*)tCtrl.pCtrlObj)->getIP());
    }
    else if (tCtrl.nType == ECTRLTYPE_IPD_TEXT)
    {
        //QByteArray byteArray = ((IpInputWidget*)tCtrl.pCtrlObj)->getIP().toLatin1();
        //strcpy(((char*)tCtrl.pValue), byteArray.data());
    }
    else if (tCtrl.nType == ECTRLTYPE_CMB)
    {
        *(string*)tCtrl.pValue = ((QComboBox*)tCtrl.pCtrlObj)->currentData().toString().toStdString();
    }
	else if (tCtrl.nType == ECTRLTYPE_CMB_INT)
	{
		*(u8*)tCtrl.pValue = ((QComboBox*)tCtrl.pCtrlObj)->currentIndex();
	}
    else if (tCtrl.nType == ECTRLTYPE_CMB_INT8)
    {
        *(u8*)tCtrl.pValue = ((QComboBox*)tCtrl.pCtrlObj)->currentIndex();
    }
    else if (tCtrl.nType == ECTRLTYPE_CMB_INT16)
    {
        *(u16*)tCtrl.pValue = ((QComboBox*)tCtrl.pCtrlObj)->currentIndex();
    }
    else if (tCtrl.nType == ECTRLTYPE_CMB_TXT)
    {
		QByteArray byteArray = ((QComboBox*)tCtrl.pCtrlObj)->currentText().toLatin1();
		*((string*)tCtrl.pValue) = byteArray.data();
    }
    else if (tCtrl.nType == EMCTRLTYPE_PUSHBTN || tCtrl.nType == EMCTRLTYPE_TOOLBTN)
    {
        *(u8*)tCtrl.pValue = ((QAbstractButton*)tCtrl.pCtrlObj)->isChecked();
    }
    else if (tCtrl.nType == EMCTRLTYPE_PUSHBTN_INT8)
    {
        *(u8*)tCtrl.pValue = ((QAbstractButton*)tCtrl.pCtrlObj)->text().toInt();
    }
    else if (tCtrl.nType == EMCTRLTYPE_PUSHBTN_FLOAT)
    {
        *(u8*)tCtrl.pValue = ((QAbstractButton*)tCtrl.pCtrlObj)->text().toFloat();
    }
    else if (tCtrl.nType == ECTRLTYPE_CHECKBOX)
    {
        *(bool*)tCtrl.pValue = ((QCheckBox*)tCtrl.pCtrlObj)->isChecked();
    }
    else if (tCtrl.nType == ECTRLTYPE_TEXTBROWSER_TXT)
    {
        QByteArray byteArray = ((QTextBrowser*)tCtrl.pCtrlObj)->toPlainText().toLatin1();
        *((string*)tCtrl.pValue) = byteArray.data();
    }
	else if (tCtrl.nType == ECTRLTYPE_TXTEDT_TXT)
	{
	    QByteArray byteArray = ((QTextEdit*)tCtrl.pCtrlObj)->toPlainText().toLatin1();
	    *((string*)tCtrl.pValue) = byteArray.data();
	}
    else
    {
        return false;
    }

    return true;
}
bool CWgtBase::UpdateUIFromData(u32 dwEvent, int nCtrlIndex)
{
	//    qDebug() << "UpdateUIFromData" << nCtrlIndex << "size" << m_mapCtrl.size();
    if (nCtrlIndex == 0)
    {
        map<int, TUIInfo>::iterator ite = m_mapCtrl.begin();
        while (ite != m_mapCtrl.end())
        {
            //           qDebug() << "ite->second" << (char*)(ite->second.pValue);
                UpdateUIFromData(ite->second);
            ite++;
        }
        return true;
    }
    else
    {
        map<int, TUIInfo>::iterator ite = m_mapCtrl.find(nCtrlIndex);
        if (ite != m_mapCtrl.end())
        {
                return UpdateUIFromData(ite->second);
        }
        return false;
    }
}
bool CWgtBase::UpdateUIFromData(TUIInfo& tCtrl)
{
	if (tCtrl.nType == EMCTRLTYPE_NULL || tCtrl.pCtrlObj == NULL || tCtrl.pValue == NULL)
    {
        return false;
    }
    //
    if (tCtrl.nType == ECTRLTYPE_LBL_TXT)
    {
        QString str = QString::fromStdString(*(string*)tCtrl.pValue);
        if (str != ((QLabel*)tCtrl.pCtrlObj)->text())
        {
            ((QLabel*)tCtrl.pCtrlObj)->setText(str);
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_INT8)
    {
        if (QString::number(*(u8*)tCtrl.pValue) != ((QLabel*)tCtrl.pCtrlObj)->text())
        {
            ((QLabel*)tCtrl.pCtrlObj)->setText(QString::number(*(u8*)tCtrl.pValue));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_INT16)
    {
        if (QString::number(*(u16*)tCtrl.pValue) != ((QLabel*)tCtrl.pCtrlObj)->text())
        {
            ((QLabel*)tCtrl.pCtrlObj)->setText(QString::number(*(u16*)tCtrl.pValue));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_INT)
    {
        if (QString::number(*(u32*)tCtrl.pValue) != ((QLabel*)tCtrl.pCtrlObj)->text())
        {
            ((QLabel*)tCtrl.pCtrlObj)->setText(QString::number(*(u32*)tCtrl.pValue));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_LBL_FLOAT)
    {
        if (*(float*)tCtrl.pValue - ((QLabel*)tCtrl.pCtrlObj)->text().toFloat() > 0.00001)
        {
            ((QLabel*)tCtrl.pCtrlObj)->setText(QString::number(*(float*)tCtrl.pValue, 'f', 5));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_TXTUTF8)
    {
		QString str = QString::fromStdString(*(string*)tCtrl.pValue);
		if (str != ((QLineEdit*)tCtrl.pCtrlObj)->text())
		{
			((QLineEdit*)tCtrl.pCtrlObj)->setText(str);
		}
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_TXT)
    {
		QString str = QString::fromStdString(*(string*)tCtrl.pValue);
		if (str != ((QLineEdit*)tCtrl.pCtrlObj)->text())
		{
			((QLineEdit*)tCtrl.pCtrlObj)->setText(str);
		}
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_INT8)
    {
        if (QString::number(*(u8*)tCtrl.pValue) != ((QLineEdit*)tCtrl.pCtrlObj)->text())
        {
            ((QLineEdit*)tCtrl.pCtrlObj)->setText(QString::number(*(u8*)tCtrl.pValue));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_INT16)
    {
        if (QString::number(*(u16*)tCtrl.pValue) != ((QLineEdit*)tCtrl.pCtrlObj)->text())
        {
            ((QLineEdit*)tCtrl.pCtrlObj)->setText(QString::number(*(u16*)tCtrl.pValue));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_INT)
    {
        if (QString::number(*(u32*)tCtrl.pValue) != ((QLineEdit*)tCtrl.pCtrlObj)->text())
        {
            ((QLineEdit*)tCtrl.pCtrlObj)->setText(QString::number(*(u32*)tCtrl.pValue));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_FLOAT)
    {
        if (*(float*)tCtrl.pValue - ((QLineEdit*)tCtrl.pCtrlObj)->text().toFloat() > 0.00001)
        {
            ((QLineEdit*)tCtrl.pCtrlObj)->setText(QString::number(*(float*)tCtrl.pValue, 'f', 5));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_EDT_FLOAT2)
    {
        if (*(float*)tCtrl.pValue - ((QLineEdit*)tCtrl.pCtrlObj)->text().toFloat() > 0.00001)
        {
            ((QLineEdit*)tCtrl.pCtrlObj)->setText(QString::number((*(float*)tCtrl.pValue, 'f', 2)));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_RADIO_INT)
    {
        QAbstractButton* pButton = ((QButtonGroup*)tCtrl.pCtrlObj)->button(*(int*)tCtrl.pValue);
        if (pButton)
        {
            pButton->setChecked(true);
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_RADIO_INT8)
    {
        QAbstractButton* pButton = ((QButtonGroup*)tCtrl.pCtrlObj)->button(*(u8*)tCtrl.pValue);
        if (pButton)
        {
            pButton->setChecked(true);
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_RADIO_INT16)
    {
        QAbstractButton* pButton = ((QButtonGroup*)tCtrl.pCtrlObj)->button(*(u16*)tCtrl.pValue);
        if (pButton)
        {
            pButton->setChecked(true);
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_IPD_INT)
    {
        //((QObject*)tCtrl.pCtrlObj)->sett(IpInt2QString((u32)*(u32*)tCtrl.pValue));
    }
    else if (tCtrl.nType == ECTRLTYPE_IPD_TEXT)
    {
        //((IpInputWidget*)tCtrl.pCtrlObj)->setIP(QString((char*)tCtrl.pValue));
    }
	else if (tCtrl.nType == ECTRLTYPE_CMB)
	{
	    int nIndex = ((QComboBox*)tCtrl.pCtrlObj)->findData(*(u16*)tCtrl.pValue);
	    ((QComboBox*)tCtrl.pCtrlObj)->setCurrentIndex(nIndex);
	}
    else if (tCtrl.nType == ECTRLTYPE_CMB_INT)
    {
        int nIndex = *(int*)tCtrl.pValue;
        ((QComboBox*)tCtrl.pCtrlObj)->setCurrentIndex(nIndex);
    }
    else if (tCtrl.nType == ECTRLTYPE_CMB_INT8)
    {
        int nIndex = *(int*)tCtrl.pValue;
        ((QComboBox*)tCtrl.pCtrlObj)->setCurrentIndex(nIndex);
    }
    else if (tCtrl.nType == ECTRLTYPE_CMB_INT16)
    {
        int nIndex = *(int*)tCtrl.pValue;
        ((QComboBox*)tCtrl.pCtrlObj)->setCurrentIndex(nIndex);
    }
    else if (tCtrl.nType == ECTRLTYPE_CMB_TXT)
    {
        QString str = QString::fromStdString(*(string*)tCtrl.pValue);
	    if (str != ((QComboBox*)tCtrl.pCtrlObj)->currentText())
	    {
		    ((QComboBox*)tCtrl.pCtrlObj)->setCurrentText(str);
	    }
    }
    else if (tCtrl.nType == EMCTRLTYPE_PUSHBTN || tCtrl.nType == EMCTRLTYPE_TOOLBTN)
    {
        bool bCheck = *(bool*)tCtrl.pValue;
        if (bCheck != ((QAbstractButton*)tCtrl.pCtrlObj)->isChecked())
        {
            ((QAbstractButton*)tCtrl.pCtrlObj)->setChecked(bCheck);
        }
    }
    else if (tCtrl.nType == EMCTRLTYPE_PUSHBTN_INT8)
    {
        if (QString::number(*(u8*)tCtrl.pValue) != ((QAbstractButton*)tCtrl.pCtrlObj)->text())
        {
            ((QAbstractButton*)tCtrl.pCtrlObj)->setText(QString::number(*(u8*)tCtrl.pValue));
        }
    }
    else if (tCtrl.nType == EMCTRLTYPE_PUSHBTN_FLOAT)
    {
        if (QString::number(*(float*)tCtrl.pValue) != ((QAbstractButton*)tCtrl.pCtrlObj)->text())
        {
            ((QAbstractButton*)tCtrl.pCtrlObj)->setText(QString::number(*(float*)tCtrl.pValue, 'f', 2));
        }
    }
    else if (tCtrl.nType == ECTRLTYPE_CHECKBOX)
    {
        bool bCheck = *(bool*)tCtrl.pValue;
        ((QCheckBox*)tCtrl.pCtrlObj)->setChecked(bCheck);
    }
    else if (tCtrl.nType == ECTRLTYPE_TEXTBROWSER_TXT)
    {
	    QString str = QString::fromStdString(*(string*)tCtrl.pValue);
	    if (str != ((QTextBrowser*)tCtrl.pCtrlObj)->toPlainText())
	    {
		    ((QTextBrowser*)tCtrl.pCtrlObj)->setText(str);
	    }
    }
	else if (tCtrl.nType == ECTRLTYPE_TXTEDT_TXT)
	{
	    QString str = QString::fromStdString(*(string*)tCtrl.pValue);
	    if (str != ((QTextEdit*)tCtrl.pCtrlObj)->toPlainText())
	    {
		    ((QTextBrowser*)tCtrl.pCtrlObj)->setText(str);
	    }
	}
    else
    {
        return false;
    }
    return true;
}

//////////////////////////////////////////////////////////////////////////////////////
// 公共接口
bool CWgtBase::Set(s8* szType, string strIn, string& strOut)
{
	return false;
}
bool CWgtBase::Set(s8* szType, string strIn)
{
    string strOut;
    return Set(szType, strIn, strOut);
}
bool CWgtBase::Get(s8* szType, string strIn, string& strOut)
{
	return false;
}
bool CWgtBase::Get(s8* szType, string& strOut)
{
    return Get(szType, "", strOut);
}

////////////////////////////////////////////////////////////////////////////////////
bool CWgtBase::IsOk()
{
    return m_bOk;
}
bool CWgtBase::IsCancel()
{
    return !m_bOk;
}
void CWgtBase::OnOk()
{
    m_bOk = true;
    if (m_byShowMode == EDLGSHOWMODE_POP || m_byShowMode == EDLGSHOWMODE_POP_SAVE)
    {
		close();
		quit();
    }
}
void CWgtBase::OnCancel() 
{
    m_bOk = false;
	if (m_byShowMode == EDLGSHOWMODE_POP || m_byShowMode == EDLGSHOWMODE_POP_SAVE)
	{
		close();
		quit();
	}
}

////////////////////////////////////////////////////////////////////////////////////
void CWgtBase::closeEvent(QCloseEvent* e)
{
	e->accept();//不会将事件传递给组件的父组件
    quit();
}
// 自定义函数模拟模态行为
void CWgtBase::exec() 
{
	setWindowModality(Qt::ApplicationModal);  // 设置为应用程序模态
    setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
	setObjectName(m_strTitle);
    setWindowTitle(m_strTitle);
    //setWindowFlags(Qt::FramelessWindowHint);
	show();  // 显示窗口

	// 启动事件循环，阻塞主线程，直到窗口关闭
	QEventLoop loop;
    connect(this, &CWgtBase::close, &loop, &QEventLoop::quit);
	connect(this, &CWgtBase::quit, &loop, &QEventLoop::quit);
	loop.exec();
}


